package com.ruoyi.order.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.blindBoxManage.mapper.BlindBoxMapper;
import com.ruoyi.blindBoxManage.mapper.BoxMapper;
import com.ruoyi.blindBoxManage.mapper.PlayTypeMapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.order.domain.BlindBoxOrder;
import com.ruoyi.order.service.IBlindBoxOrderService;
import com.ruoyi.userManage.mapper.UserMapper;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

/**
 * 盲盒订单Controller
 *
 * @author ruoyi
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/blindBoxOrder/blindBoxOrder")
public class BlindBoxOrderController extends BaseController {
    @Autowired
    private IBlindBoxOrderService blindBoxOrderService;
    @Resource
    private PlayTypeMapper playTypeMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private BlindBoxMapper blindBoxMapper;
    @Resource
    private BoxMapper boxMapper;
    /**
     * 查询盲盒订单列表
     */
    @PreAuthorize("@ss.hasPermi('blindBoxOrder:blindBoxOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(BlindBoxOrder blindBoxOrder) {
        if (!StrUtil.isEmpty(blindBoxOrder.getPlayTypeName())) {
            Integer playTypeId = playTypeMapper.selectIdByName(blindBoxOrder.getPlayTypeName());
            if (playTypeId != null) {
                blindBoxOrder.setPlayTypeId(playTypeId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        if (!StrUtil.isEmpty(blindBoxOrder.getBlindBoxName())) {
            Long blindId = blindBoxMapper.selectIdByName(blindBoxOrder.getBlindBoxName());
            if (blindId != null) {
                blindBoxOrder.setBlindBoxId(blindId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        if (!StrUtil.isEmpty(blindBoxOrder.getBoxName())) {
            Long boxId = boxMapper.selectIdByName(blindBoxOrder.getBoxName());
            if (boxId != null) {
                blindBoxOrder.setBoxId(boxId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        if (!StrUtil.isEmpty(blindBoxOrder.getUserPhone())) {
            Long userId = userMapper.selectIdByPhone(blindBoxOrder.getUserPhone());
            if (userId != null) {
                blindBoxOrder.setUserId(userId);
            } else {
                return getDataTable(Collections.emptyList());
            }
        }
        startPage();
        List<BlindBoxOrder> list = blindBoxOrderService.selectBlindBoxOrderList(blindBoxOrder);
        if (!CollectionUtils.isEmpty(list)) {
            for (BlindBoxOrder order : list) {
                String blindBoxName = blindBoxMapper.selectNameById(order.getBlindBoxId());
                order.setBlindBoxName(!StrUtil.isEmpty(blindBoxName) ? blindBoxName : "");
                String boxName = boxMapper.selectNameById(order.getBoxId());
                order.setBoxName(!StrUtil.isEmpty(boxName) ? boxName : "");
                String phone = userMapper.selectPhoneById(order.getUserId());
                order.setUserPhone(!StrUtil.isEmpty(phone) ? phone : "");
                String playTypeName = playTypeMapper.selectNameById(order.getPlayTypeId());
                order.setPlayTypeName(!StrUtil.isEmpty(playTypeName) ? playTypeName : "");
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出盲盒订单列表
     */
    @PreAuthorize("@ss.hasPermi('blindBoxOrder:blindBoxOrder:export')")
    @Log(title = "盲盒订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BlindBoxOrder blindBoxOrder) {
        if (!StrUtil.isEmpty(blindBoxOrder.getPlayTypeName())) {
            Integer playTypeId = playTypeMapper.selectIdByName(blindBoxOrder.getPlayTypeName());
            if (playTypeId != null) {
                blindBoxOrder.setPlayTypeId(playTypeId);
            }
        }
        if (!StrUtil.isEmpty(blindBoxOrder.getBlindBoxName())) {
            Long blindId = blindBoxMapper.selectIdByName(blindBoxOrder.getBlindBoxName());
            if (blindId != null) {
                blindBoxOrder.setBlindBoxId(blindId);
            }
        }
        if (!StrUtil.isEmpty(blindBoxOrder.getBoxName())) {
            Long boxId = boxMapper.selectIdByName(blindBoxOrder.getBoxName());
            if (boxId != null) {
                blindBoxOrder.setBoxId(boxId);
            }
        }
        if (!StrUtil.isEmpty(blindBoxOrder.getUserPhone())) {
            Long userId = userMapper.selectIdByPhone(blindBoxOrder.getUserPhone());
            if (userId != null) {
                blindBoxOrder.setUserId(userId);
            }
        }
        List<BlindBoxOrder> list = blindBoxOrderService.selectBlindBoxOrderList(blindBoxOrder);
        if (!CollectionUtils.isEmpty(list)) {
            for (BlindBoxOrder order : list) {
                String blindBoxName = blindBoxMapper.selectNameById(order.getBlindBoxId());
                order.setBlindBoxName(!StrUtil.isEmpty(blindBoxName) ? blindBoxName : "");
                String boxName = boxMapper.selectNameById(order.getBoxId());
                order.setBoxName(!StrUtil.isEmpty(boxName) ? boxName : "");
                String phone = userMapper.selectPhoneById(order.getUserId());
                order.setUserPhone(!StrUtil.isEmpty(phone) ? phone : "");
                String playTypeName = playTypeMapper.selectNameById(order.getPlayTypeId());
                order.setPlayTypeName(!StrUtil.isEmpty(playTypeName) ? playTypeName : "");
            }
        }
        ExcelUtil<BlindBoxOrder> util = new ExcelUtil<BlindBoxOrder>(BlindBoxOrder.class);
        util.exportExcel(response, list, "盲盒订单数据");
    }

    /**
     * 获取盲盒订单详细信息
     */
    @PreAuthorize("@ss.hasPermi('blindBoxOrder:blindBoxOrder:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(blindBoxOrderService.selectBlindBoxOrderById(id));
    }

    /**
     * 新增盲盒订单
     */
    @PreAuthorize("@ss.hasPermi('blindBoxOrder:blindBoxOrder:add')")
    @Log(title = "盲盒订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BlindBoxOrder blindBoxOrder) {
        return toAjax(blindBoxOrderService.insertBlindBoxOrder(blindBoxOrder));
    }

    /**
     * 修改盲盒订单
     */
    @PreAuthorize("@ss.hasPermi('blindBoxOrder:blindBoxOrder:edit')")
    @Log(title = "盲盒订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BlindBoxOrder blindBoxOrder) {
        return toAjax(blindBoxOrderService.updateBlindBoxOrder(blindBoxOrder));
    }

    /**
     * 删除盲盒订单
     */
    @PreAuthorize("@ss.hasPermi('blindBoxOrder:blindBoxOrder:remove')")
    @Log(title = "盲盒订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(blindBoxOrderService.deleteBlindBoxOrderByIds(ids));
    }
}
